package com.github.linmt.dbdoc4j.metadata.type.mysql;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.JDBCType;
import java.sql.Timestamp;
import java.util.Date;

/**
 * User: Linmt
 * Date: 2022/12/10
 * Time: 17:20
 * Description: No Description
 */
public enum MysqlTypeMappingEnum {
    DECIMAL("DECIMAL", 3, BigDecimal.class),
    DECIMAL_UNSIGNED("DECIMAL UNSIGNED", 3, BigDecimal.class),
    TINYINT("TINYINT", -6, Integer.class),
    TINYINT_UNSIGNED("TINYINT UNSIGNED", -6, Integer.class),
    BOOLEAN("BOOLEAN", 16, Boolean.class),
    SMALLINT("SMALLINT", 5, Integer.class),
    SMALLINT_UNSIGNED("SMALLINT UNSIGNED", 5, Integer.class),
    INT("INT", 4, Integer.class),
    INT_UNSIGNED("INT UNSIGNED", 4, Long.class),
    FLOAT("FLOAT", 7, Float.class),
    FLOAT_UNSIGNED("FLOAT UNSIGNED", 7, Float.class),
    DOUBLE("DOUBLE", 8, Double.class),
    DOUBLE_UNSIGNED("DOUBLE UNSIGNED", 8, Double.class),
    NULL("NULL", 0, Object.class),
    TIMESTAMP("TIMESTAMP", 93, Timestamp.class),
    BIGINT("BIGINT", -5, Long.class),
    BIGINT_UNSIGNED("BIGINT UNSIGNED", -5, BigInteger.class),
    MEDIUMINT("MEDIUMINT", 4, Integer.class),
    MEDIUMINT_UNSIGNED("MEDIUMINT UNSIGNED", 4, Integer.class),
    DATE("DATE", 91, Date.class),
    TIME("TIME", 92, Date.class),
    DATETIME("DATETIME", 93, Date.class),
    YEAR("YEAR", 91, Date.class),
    VARCHAR("VARCHAR", 12, String.class),
    VARBINARY("VARBINARY", -3, (Class) null),
    BIT("BIT", -7, Boolean.class),
    JSON("JSON", -1, String.class),
    ENUM("ENUM", 1, String.class),
    SET("SET", 1, String.class),
    TINYBLOB("TINYBLOB", -3, (Class) null),
    TINYTEXT("TINYTEXT", 12, String.class),
    MEDIUMBLOB("MEDIUMBLOB", -4, (Class) null),
    MEDIUMTEXT("MEDIUMTEXT", -1, String.class),
    LONGBLOB("LONGBLOB", -4, (Class) null),
    LONGTEXT("LONGTEXT", -1, String.class),
    BLOB("BLOB", -4, (Class) null),
    TEXT("TEXT", -1, String.class),
    CHAR("CHAR", 1, String.class),
    BINARY("BINARY", -2, (Class) null),
    GEOMETRY("GEOMETRY", -2, (Class) null),
    UNKNOWN("UNKNOWN", 1111, (Class) null);

    public final String name;
    public int jdbcType;
    public final Class<?> javaClass;

    public static MysqlTypeMappingEnum getByName(String fullMysqlTypeName) {
        String typeName = "";
        if (fullMysqlTypeName.indexOf("(") != -1) {
            typeName = fullMysqlTypeName.substring(0, fullMysqlTypeName.indexOf("(")).trim();
        } else {
            typeName = fullMysqlTypeName;
        }

        typeName = typeName.toUpperCase();
        return MysqlTypeMappingEnum.valueOf(typeName);
    }

    MysqlTypeMappingEnum(String name, int jdbcType, Class<?> javaClass) {
        this.name = name;
        this.jdbcType = jdbcType;
        this.javaClass = javaClass;
    }

    public JDBCType getJdbcType() {
        return JDBCType.valueOf(this.jdbcType);
    }
}
